function [BF,BEF,Tc] = HC(FN,EF,Tp,train_t_input,train_v_input,train_t_target,train_v_target, algorithm0)
%HC Hill Climbing
%   Detailed explanation goes here
%FN Best feature subset until now
%EF The average precistion of the best feature subset FN
%  train_t_input��training dataset
%  train_v_input:Validation dataset
%  train_t_target:Training label
%  train_v_target:Validation label

%Output
%FN Best feature subset until now
%EF The fitness of the best feature subset FN
if (Tp > 0)
    N = size(FN,2);
    %feature subsets.
    FS = [];
    for i=1:N
        U = FN;
        U(i) = ~U(i);
        FS(i,:) = U;
    end
    
    %Evaluate the fitness(Average Precision) the new solutions in FS to see if there are better solutions than
    %FN��
    
    u_target = train_t_target;
    v_target = train_v_target;
    E = [];
    
    k = 0;
    U = [];
    for i=1:N
        if Tp>0
            t1 = clock;
            feasub = FS(i,:);
            din = find(feasub);
            u = train_t_input(:,din);
            v = train_v_input(:,din);
            
%             [Prior,PriorN,Cond,CondN]=MLKNN_train(u,u_target',10,1);
%             [HammingLoss,RankingLoss,OneError,Coverage,Average_Precision,Outputs,Pre_Labels]=MLKNN_test(u,u_target',v,v_target',10,Prior,PriorN,Cond,CondN);
%             EM = Average_Precision;
            algorithm0.build(u, u_target);
            result_i = algorithm0.apply(u, u_target);
            EM = AveragePrecision.calc(u_target, result_i.Y_hat);
            
            if (EM > EF)
                k = k+1;
                U(k,:) = FS(i,:);
                E(k,:) = EM;
            end;
            t2 = clock;
            Ts = etime(t2,t1);
            Tp = Tp - Ts;
            Tc = Tp;
        else
            break
        end
    end
    
    if (k>0 && Tc>0)
        V = zeros(k,1);
        for i=1:k
            [FN,BEF,Tc] = HC(U(i,:),E(i),Tc,train_t_input,train_v_input,train_t_target,train_v_target);
            V(i) = BEF;
        end
        [BEF,V_index] = min(V);
        BF = U(V_index,:);
    else
        BF = FN;
        BEF = EF;
        return
    end
else
    BF = FN;
    BEF = EF;
    Tc = Tp;
    return
end

end
